<!--
 * @Description: 合同计划
 * @Author: wind-lc
 * @version: 1.0
 * @Date: 2023-05-25 17:48:06
 * @LastEditTime: 2023-06-14 09:56:59
 * @FilePath: \cscec-pms-admin\src\views\construction\contractor\index.vue
-->

<template>
  <base-breadcrumb>
    <!-- 搜索 -->
    <searchData
      :search-data="searchOption"
      @search="search"
    />
    <!-- 搜索 -->
    <a-card class="card-wrapper">
      <!-- <title-name title="内控计划列表" /> -->
      <a-button
        v-btnPermission="'pm_067_exp_04'"
        type="primary"
        class="export-btn"
        :loading="exporting"
        :disabled="total<1"
        @click="exportList"
      >导出全部</a-button>
      <div class="pub-table-select-count">
        <a-icon
          type="info-circle"
          theme="twoTone"
        />
        <span style="margin-right: 12px">
          总共：<span class="totalNum">{{ total }}</span>
        </span>
      </div>
      <s-table
        ref="table"
        row-key="orgId"
        :columns="columns"
        :data="loadData"
        show-size-changer
        show-quick-jumper
        :scroll="scroll"
        :show-pagination="false"
        :children-column-name="'childTree'"
        :expanded-row-keys="expandedRowKeys"
        @expand="expandedRowsChange"
      >
        <template
          slot="rate"
          slot-scope="text"
        >
          <a-progress
            stroke-linecap="round"
            :percent="text"
            status="active"
          >
            <template #format="text">
              <span>{{ text }}%</span>
            </template>
          </a-progress>
        </template>
        <template
          slot="statusCode"
          slot-scope="text, record"
        >
          <a-badge
            v-if="record.tageCode === 'PD'"
            :status="record.projectVO.status | statusTypeFilter"
            :text="record.projectVO.status | statusFilter"
          />
        </template>
        <template
          slot="operation"
          slot-scope="text, record"
        >
          <MoreButton>
            <a
              v-if="record.tageCode == 'PD'"
              v-btnPermission="'pm_067_loo_01'"
              @click="handleDetail(record)"
            >查看</a>
            <a
              v-if="record.tageCode == 'PD'"
              v-btnPermission="'pm_067_exp_05'"
              @click="handleExport(record)"
            >导出</a>
            <template v-if="record.tageCode == 'PD' && (record.projectVO.status== '0' || record.projectVO.status=='3') ">
              <a
                v-btnPermission="'pm_067_edi_02'"
                @click="handleUpdate(record)"
              >编辑</a>
            </template>
            <template v-if="record.tageCode == 'PD' && ( record.projectVO.status== '2' || record.projectVO.status=='4')">
              <a
                v-btnPermission="'pm_067_imp_03'"
                @click="handleImplementationManagement(record)"
              >实施管理</a>
            </template>
            <template v-if="record.tageCode == 'PD' && record.projectVO.status!='0'">
              <a
                v-btnPermission="'pm_067_loo_01'"
                @click="$bus.$emit('showApprovalDetailsDialog', record.projectVO.scheduleContractPlanId)"
              >审批信息</a>
            </template>
          </MoreButton>
        </template>
      </s-table>
    </a-card>
    <!-- 导出 -->
    <PlanExport
      :visible.sync="visible"
      :downloadFun="downloadFun"
    />
    <!-- 导出 -->
  </base-breadcrumb>
</template>
<script>
import { searchOption, columns, statusMap } from './model.js'
import contractor from '@/api/construction/contractor'
import constructionSchedule from '@/api/construction/constructionSchedule'
import MoreButton from '@/components/MoreButton'
import PlanExport from '@/views/construction/components/PlanExport'
export default {
  name: 'contractor',
  components: {
    MoreButton,
    PlanExport
  },
  filters: {
    statusFilter(type) {
      return statusMap[type] && statusMap[type].text
    },
    statusTypeFilter(type) {
      return statusMap[type] && statusMap[type].status
    }
  },
  data() {
    return {
      // 查询参数
      searchData: {},
      // 搜索选项
      searchOption,
      // 表头
      columns,
      // 统计
      total: 0,
      // 默认展开项
      expandedRowKeys: [],
      // 导出模态框隐藏显示
      visible: false,
      // 导出时传入接口
      downloadFun: {},
      // 表格滚动纵横
      scroll: { x: 2020, y: 450 },
      // 导出中
      exporting: false
    }
  },
  created() {
    this.getWbsTemplateList()
  },
  mounted() {
    this.setTableHeight()
  },
  methods: {
    /**
     * @description: 设置滚动条Y方向
     * @return {void}
     */
    setTableHeight() {
      this.scroll.y = document.querySelector('.content-bx').clientHeight - 195 || 450
    },
    /**
     * @description: 获取项目业态
     * @param {void}
     * @return {void}
     */
    getWbsTemplateList() {
      constructionSchedule.wbstemplate({ pageNo: 1, pageSize: 2000 }).then((res) => {
        this.searchOption[1].data = res.data.map((item) => {
          return {
            label: item.formatName,
            value: item.formatId
          }
        })
      })
    },
    /**
     * @description: 导出全部
     * @return {void}
     */
    async exportList() {
      try {
        this.exporting = true
        this.handleDownload(await contractor.contractPlanExportList(this.searchData))
      } catch (error) {
        console.log(error)
      } finally {
        this.exporting = false
      }
    },
    /**
     * @description: 下载
     * @return {void}
     */
    handleDownload(res) {
      const blob = new Blob([res.data])
      const filename = decodeURIComponent(res.headers['content-disposition'].split('filename=')[1])
      const downloadElement = document.createElement('a')
      const href = window.URL.createObjectURL(blob) // 创建下载的链接
      downloadElement.href = href
      downloadElement.download = filename // 获取文件名
      document.body.appendChild(downloadElement)
      downloadElement.click() //点击下载
      document.body.removeChild(downloadElement) //下载完成移除元素
      window.URL.revokeObjectURL(href) //释放blob对象
    },
    /**
     * @description: 表格数据加载方法
     * @param {Object} parameter 分页信息
     * @return {Promise} 接口Promise
     */
    async loadData(parameter) {
      try {
        const {
          data: { treeVO, total, expandedRowKeys }
        } = await contractor.queryScheduleContractPlanTree(this.searchData)
        this.total = total
        this.expandedRowKeys = expandedRowKeys
        return Promise.resolve({
          data: treeVO ? [treeVO] : []
        })
      } catch (error) {
        console.log(error)
        return Promise.reject(error)
      }
    },
    /**
     * @description: 搜索
     * @param {Object} value 搜索数据
     * @return {Void}
     */
    search(value) {
      this.searchData = value || {}
      this.$refs.table.refresh(true)
    },
    /**
     * @description: 查看
     * @param {object} record 行数据
     * @return {void}
     */
    handleDetail({ projectVO: { projectId, status, scheduleContractPlanId: planId } }) {
      this.$router.push({
        path: '/construction/contractor/contractorView',
        query: {
          projectId,
          planId,
          pageType: 'detail',
          status
        }
      })
    },
    /**
     * @description: 编辑
     * @param {object} record 行数据
     * @return {void}
     */
    handleUpdate({ projectVO: { projectId, status, scheduleContractPlanId: planId } }) {
      this.$router.push({
        path: '/construction/contractor/contractorView',
        query: {
          projectId,
          pageType: 'update',
          planId,
          status
        }
      })
    },
    /**
     * @description: 实施管理
     * @param {object} record 行数据
     * @return {void}
     */
    handleImplementationManagement({ projectVO: { projectId, status, scheduleContractPlanId: planId } }) {
      this.$router.push({
        path: '/construction/contractor/contractorImplementationManagement',
        query: {
          projectId,
          status,
          planId
        }
      })
    },
    /**
     * @description: 手动展开
     * @param {boolean} expanded 是否展开
     * @param {object} record 行
     * @return {void}
     */
    expandedRowsChange(expanded, record) {
      if (expanded) {
        this.expandedRowKeys.push(record.orgId)
      } else {
        this.expandedRowKeys.splice(
          this.expandedRowKeys.findIndex((el) => el === record.orgId),
          1
        )
      }
    },
    /**
     * @description: 导出
     * @param {object} record 行
     * @return {void}
     */
    handleExport(record) {
      this.visible = true
      // 导出接口
      this.downloadFun = {
        project: () => {
          return contractor.contractPlanExportProject(record.projectVO.scheduleContractPlanId)
        },
        excel: () => {
          return contractor.contractPlanExportExcl(record.projectVO.scheduleContractPlanId)
        }
      }
    }
  }
}
</script>
<style lang="less" scoped>
.card-wrapper {
  height: calc(100% - 82px);
  margin-bottom: 0;
}
.export-btn {
  margin-bottom: 10px;
}
</style>

